package com.ems.sys.controller;
import com.ems.entity.Result;
import com.ems.entity.StatusCode;
import com.ems.sys.pojo.Position;
import com.ems.sys.service.PositionService;
import com.github.pagehelper.PageInfo;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/****
 * @Author:传智播客
 * @Description:
 * @Date 2019/6/14 0:18
 *****/

@RestController
@RequestMapping("/sys/position")
//@CrossOrigin
public class PositionController {

    @Autowired
    private PositionService positionService;

    /***
     * Position分页条件搜索实现
     * @param position
     * @param page
     * @param size
     * @return
     */
    @PostMapping(value = "/search/{page}/{size}" )
    public Result<PageInfo> findPage(@RequestBody(required = false)  Position position, @PathVariable  int page, @PathVariable  int size){
        //调用PositionService实现分页条件查询Position
        PageInfo<Position> pageInfo = positionService.findPage(position, page, size);
        return new Result(true, StatusCode.OK,"查询成功",pageInfo);
    }

    /***
     * Position分页搜索实现
     * @param page:当前页
     * @param size:每页显示多少条
     * @return
     */
    @GetMapping(value = "/search/{page}/{size}" )
    public Result<PageInfo> findPage(@PathVariable  int page, @PathVariable  int size){
        //调用PositionService实现分页查询Position
        PageInfo<Position> pageInfo = positionService.findPage(page, size);
        return new Result<PageInfo>(true,StatusCode.OK,"查询成功",pageInfo);
    }

    /***
     * 多条件搜索品牌数据
     * @param position
     * @return
     */
    @PostMapping(value = "/search" )
    public Result<List<Position>> findList(@RequestBody(required = false)  Position position){
        //调用PositionService实现条件查询Position
        List<Position> list = positionService.findList(position);
        return new Result<List<Position>>(true,StatusCode.OK,"查询成功",list);
    }

    /***
     * 根据ID删除品牌数据
     * @param id
     * @return
     */
    @DeleteMapping(value = "/{id}" )
    public Result delete(@PathVariable String id){
        //调用PositionService实现根据主键删除
        positionService.delete(id);
        return new Result(true,StatusCode.OK,"删除成功");
    }

    /***
     * 修改Position数据
     * @param position
     * @param id
     * @return
     */
    @PutMapping(value="/{id}")
    public Result update(@RequestBody  Position position,@PathVariable String id){
        //设置主键值
        position.setPNum(id);
        //调用PositionService实现修改Position
        positionService.update(position);
        return new Result(true,StatusCode.OK,"修改成功");
    }

    /***
     * 新增Position数据
     * @param position
     * @return
     */
    @PostMapping
    public Result add(@RequestBody   Position position){
        //调用PositionService实现添加Position
        positionService.add(position);
        return new Result(true,StatusCode.OK,"添加成功");
    }

    /***
     * 根据ID查询Position数据
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result<Position> findById(@PathVariable String id){
        //调用PositionService实现根据主键查询Position
        Position position = positionService.findById(id);
        return new Result<Position>(true,StatusCode.OK,"查询成功",position);
    }

    /***
     * 查询Position全部数据
     * @return
     */
    @GetMapping
    public Result<List<Position>> findAll(){
        //调用PositionService实现查询所有Position
        List<Position> list = positionService.findAll();
        return new Result<List<Position>>(true, StatusCode.OK,"查询成功",list) ;
    }
}
